export class AttackCreepFlagTask implements ICreepTask{
  run(creep: Creep): void {

    if (creep.hits < creep.hitsMax * 0.9 && creep.body.some(part => part.type === HEAL)) {
      creep.heal(creep);
    }

    if(!creep.memory.flagNameTarget){
      return;
    }


    const flag = Game.flags[creep.memory.flagNameTarget];

    if(flag){

      // 判断是否配备了医疗兵
      const healCreeps = Object.values(Memory.creeps).filter((c)=> c.role === "heal_flag" && c.flagNameTarget === `医疗:${creep.memory.bornRoom}`);
      if(healCreeps.length === 0){
        creep.say('Not Heal');
        return;
      }

      const range = 3;
      if(creep.pos.inRangeTo(flag,range)){
        let target:any | null = null;
        const hostileCreeps = creep.pos.findInRange(FIND_HOSTILE_CREEPS,range);
        const hostileStructures = creep.pos.findInRange(FIND_HOSTILE_STRUCTURES,range);

        if(hostileCreeps.length > 0){
          target = hostileCreeps[0];
        }
        else if(hostileStructures.length > 0){
          target = hostileStructures[0];
        }

        if(creep.attack(target) === ERR_NOT_IN_RANGE){
          creep.go(target);
        }
      }
      else {
        creep.go(flag);
      }
    }
  }
}